<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="author" content="Zhou Wei <zromyk@163.com>">
  <title>C++神奇用法</title>
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="stylesheet" href="/style/pure.css">
  <link rel="stylesheet" href="/style/main.css">
  <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
  <link rel="stylesheet" href="https://apps.bdimg.com/libs/highlight.js/9.1.0/styles/default.min.css">
<link rel="stylesheet" href="/style/pell-1.0.6/dist/pell.css">

</head>
<body>
  <div id="menu-background"></div>
  <div id="menu">
    <div class="pure-menu pure-menu-horizontal">
  <div id="menu-block">
    <ul class="pure-menu-list">
      <a class="pure-menu-heading" href="/index.html">ZROMYK</a>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/index.html">主页</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/archive/index.html">归档</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/download/index.html">下载</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/feedback/index.html">反馈</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="/public/about/index.html">关于我</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" href="https://github.com/zromyk"><i class="fa fa-github" style="font-size:32px"></i></a>
</li>

    </ul>
  </div>
</div>

  </div>
  <div id="layout">
    <div class="content">
      <div id="content-articles">
  <h1 id="C++神奇用法" class="content-subhead">C++神奇用法</h1>
  <p>
    <span>2022-05-09</span>
    <span><span class="post-category post-category-cpp">C++</span></span>
    <button id="button-markdownEditor" class="pure-button" onclick="markdownEditor()">启用编辑</button>
    <button id="button-save" class="pure-button" onclick="save()">保存</button>
  </p>
  <div id="content-articles-markdownEditor" style="display: none;">
    <h1>编辑 power by pell</h1>
    <div id="editor" class="pell"></div>
    <div style="margin-top:20px;">
        <h3>Text output:</h3>
        <div id="text-output"></div>
    </div>
    <div style="margin-top:20px;">
        <h3>HTML output:</h3>
        <pre id="html-output"></pre>
    </div>
  </div>
  <div id="content-articles-markdown">
    <h3 id="println">可变长的 println</h3>
<h4 id="param-pack-c11">param pack (C++11)</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">#if __cplusplus &lt; 201103L
    #error should use C++ 11 implementation
#endif

#include &lt;iostream&gt;

template&lt;typename T&gt;
void println(T t) {
    std::cout &lt;&lt; t &lt;&lt; std::endl;
}

template&lt;typename T, typename ...U&gt;
void println(T t, U... u) {
    std::cout &lt;&lt; t &lt;&lt; &quot;, &quot;;
    println(u...);
}

int main() {
    std::cout &lt;&lt; __cplusplus &lt;&lt; std::endl;
    println(1, 2.0, '3', &quot;4&quot;);
    return 0;
}
</code></span></code></pre>
<h4 id="param-pack-if-constexpr-c17">param pack &amp; if constexpr (C++17)</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">#if __cplusplus &lt; 201703L
    #error should use C++ 17 implementation
#endif

#include &lt;iostream&gt;

template&lt;typename T, typename ...U&gt;
void println(T t, U... u) {
    if constexpr (sizeof...(U) == 0) {
        std::cout &lt;&lt; t &lt;&lt; std::endl;
    }
    else {
        std::cout &lt;&lt; t &lt;&lt; &quot;, &quot;;
        println(u...);
    }
}

int main() {
    std::cout &lt;&lt; __cplusplus &lt;&lt; std::endl;
    println(1, 2.0, '3', &quot;4&quot;);
    return 0;
}
</code></span></code></pre>
<h4 id="pack-fold-expression-c17">pack fold expression (C++17)</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">#if __cplusplus &lt; 201703L
    #error should use C++ 17 implementation
#endif

#include &lt;iostream&gt;

template&lt;typename ...U&gt;
void println(U... u) {
    ((std::cout &lt;&lt; u &lt;&lt; &quot;, &quot;), ...) &lt;&lt; std::endl;
}

int main() {
    std::cout &lt;&lt; __cplusplus &lt;&lt; std::endl;
    println(1, 2.0, '3', &quot;4&quot;);
    return 0;
}
</code></span></code></pre>
<h4 id="pack-fold-expression-c17_1">pack fold expression &amp; 三元表达式 (C++17)</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">#if __cplusplus &lt; 201703L
    #error should use C++ 17 implementation
#endif

#include &lt;iostream&gt;

template&lt;typename ...U&gt;
void println(U... u) {
    size_t i = 0; ((std::cout &lt;&lt; u &lt;&lt; (++i &lt; sizeof...(u) ? &quot;, &quot; : &quot;\n&quot;)), ...);
}

int main() {
    std::cout &lt;&lt; __cplusplus &lt;&lt; std::endl;
    println(1, 2.0, '3', &quot;4&quot;);
    return 0;
}
</code></span></code></pre>
<h4 id="pack-fold-expression-template-lambda-c20">pack fold expression &amp; template Lambda 表达式 (C++20)</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">#if __cplusplus &lt; 202002L
    #error should use C++ 20 implementation
#endif

#include &lt;iostream&gt;

template&lt;typename ...U&gt;
void println(U... u) {
    size_t i = 0;
    auto printer = [&amp;]&lt;typename Arg&gt;(Arg arg) {
        std::cout &lt;&lt; arg &lt;&lt; (++i &lt; sizeof...(U) ? &quot;, &quot; : &quot;\n&quot;);
    };
    (printer(u), ...);
}

int main() {
    std::cout &lt;&lt; __cplusplus &lt;&lt; std::endl;
    println(1, 2.0, '3', &quot;4&quot;);
    return 0;
}
</code></span></code></pre>
<h4 id="c17">起飞 (C++17)</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">#if __cplusplus &lt; 201703L
    #error should use C++ 17 implementation
#endif

#include &lt;iostream&gt;

template&lt;typename ...U&gt;
auto sum(U... u) {
    // return (u + ...);
    return (... + u);
}

template&lt;auto ...value&gt;
auto sum() {
    // return (value + ...);
    return (... + value);
}

template&lt;typename ...U&gt;
void println(U... u) {
    size_t i = 0; ((std::cout &lt;&lt; u &lt;&lt; (++i &lt; sizeof...(u) ? &quot;, &quot; : &quot;\n&quot;)), ...);
}

int main() {
    std::cout &lt;&lt; __cplusplus &lt;&lt; std::endl;
    println(sum(1));
    println(sum(1, 2, 3));
    println(sum(std::string(&quot;1&quot;), std::string(&quot;2&quot;), std::string(&quot;3&quot;)));
    println(sum&lt;1&gt;());
    println(sum&lt;1, 2, 3&gt;());
    return 0;
}
</code></span></code></pre>
<h4 id="stdcommontypet-c17">std::common<em>type</em>t 共用类型 (C++17)</h4>
<pre><code class="pre-wrap"><span style="overflow-x: auto; max-width:100%; display:inline;"><code class="language-c++">#if __cplusplus &lt; 201703L
    #error should use C++ 17 implementation
#endif

#include &lt;iostream&gt;

template&lt;typename ...U&gt;
auto getNthElement(size_t n, U... u) {
    using commType_t = std::common_type_t&lt;U...&gt;;
    commType_t ret;
    size_t i = 0; (((i++ == n) &amp;&amp; (ret = u, true)) || ...);
    // std::cout &lt;&lt; typeid(ret).name() &lt;&lt; std::endl;
    return ret;
}

template&lt;typename ...U&gt;
void println(U... u) {
    size_t i = 0; ((std::cout &lt;&lt; u &lt;&lt; (++i &lt; sizeof...(u) ? &quot;, &quot; : &quot;\n&quot;)), ...);
}

int main() {
    std::cout &lt;&lt; __cplusplus &lt;&lt; std::endl;
    println(getNthElement(2, 1, 2.0, 3.0f, 4.0, 5L));
    // println(getNthElement(2, &quot;1&quot;, &quot;2.0&quot;, 3.0f, 4.0, 5L));
    return 0;
}
</code></span></code></pre>
  </div>
</div>
<div id="nav">
    <div class="navigation">
  <ul class="pure-menu-list">
    <li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.25em;" href="#println">可变长的 println</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#param-pack-c11">param pack (C++11)</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#param-pack-if-constexpr-c17">param pack &amp; if constexpr (C++17)</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#pack-fold-expression-c17">pack fold expression (C++17)</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#pack-fold-expression-c17_1">pack fold expression &amp; 三元表达式 (C++17)</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#pack-fold-expression-template-lambda-c20">pack fold expression &amp; template Lambda 表达式 (C++20)</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#c17">起飞 (C++17)</a>
</li>
<li class="pure-menu-item">
  <a class="pure-menu-link" style="padding:0.1em 0em 0.1em 1.75em;" href="#stdcommontypet-c17">std::common<em>type</em>t 共用类型 (C++17)</a>
</li>

  </ul>
</div>

</div>
 
    </div>
  </div>
  <div id="footer-background">
    <div id="footer">
      <div class="legal pure-g">
  <div class="pure-u-1 u-sm-1-2">
    <p class="legal-license"><a href="https://beian.miit.gov.cn/#/Integrated/index">浙ICP备2020038748号</a></p>
  </div>
  <div class="pure-u-1 u-sm-1-2">
    <p class="legal-links"><a href="https://github.com/zromyk">GitHub</a></p>
    <p class="legal-copyright">Copyright © 2021 Wei Zhou. 保留所有权利。</p>
  </div>
</div>
    </div>
  </div>
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn1.lncld.net/static/js/2.5.0/av-min.js"></script>
  <script src='/style/Valine.min.js'></script>
  <script src='/style/latest.js?config=TeX-MML-AM_CHTML'></script>
<script src="https://cdn.geogebra.org/apps/deployggb.js"></script>
<script src="https://apps.bdimg.com/libs/highlight.js/9.1.0/highlight.min.js"></script>
<script type="text/javascript">
  hljs.initHighlightingOnLoad();
</script>
<script src="/style/pell-1.0.6/dist/pell.js"></script>
<script>
  var editor = window.pell.init({
    element: document.getElementById('editor'),
    defaultParagraphSeparator: 'p',
    onChange: function(html) {
        document.getElementById('text-output').innerHTML = html
        document.getElementById('html-output').textContent = html
    }
  });
</script>
<script>
  function markdownEditor() {
    var articles = document.getElementById('content-articles-markdown');
    if (articles.getAttribute("contenteditable") == "true") {
        articles.setAttribute("contenteditable", "false");
        document.getElementById("content-articles-markdownEditor").style.display = "none"; //隐藏
        document.getElementById("button-markdownEditor").innerHTML = "启用编辑";
    } else {
        articles.setAttribute("contenteditable", "true");
        document.getElementById("content-articles-markdownEditor").style.display = ""; //显示
        document.getElementById("button-markdownEditor").innerHTML = "关闭编辑";
    }
  };
</script>
<script>
  function save() {
      window.alert("保存成功");
  };
</script>

</body>
</html>
